package com.youga.xuapplication.ui.fragment;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.youga.xuapplication.R;
import com.youga.xuapplication.app.XuApplication;
import com.youga.xuapplication.config.HttpConstant;
import com.youga.xuapplication.http.GsonRequest;
import com.youga.xuapplication.model.Cost;
import com.youga.xuapplication.model.Tour;
import com.youga.xuapplication.model.User;

import net.cgsoft.widget.https.HttpCallBack;
import net.cgsoft.widget.model.Model;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Youga on 2016/1/18.
 */
public class CalculatorFragment extends Fragment implements HttpConstant {

    private static final String TAG = "CalculatorFragment";
    @Bind(R.id.spinner)
    Spinner spinner;
    @Bind(R.id.branch)
    EditText branch;
    @Bind(R.id.detail)
    TextView detail;
    private User mUser;
    private GsonRequest mRequest;
    private TourAdapter mTourAdapter;

    public static CalculatorFragment newInstance() {
        CalculatorFragment fragment = new CalculatorFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequest = new GsonRequest(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_calculator, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUser = XuApplication.application.getUser();
        Cost cost = mUser.getCost();
        mTourAdapter = new TourAdapter(null);
        spinner.setAdapter(mTourAdapter);
        detail.setText("固定成本:" + cost.getFixedCost()+ "\n变动成本:" + cost.getVariableCost());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        obtainCityList();
    }

    private void obtainCityList() {
        HashMap<String, String> params = new HashMap<>();
        params.put("action", "TOUR_LIST");
        mRequest.function(CITY_LIST, params, new TypeToken<Model<Tour>>() {
        }, new HttpCallBack<Tour>() {

            @Override
            public boolean onStart() {
                return false;
            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onSuccess(Model<Tour> model) {
                ArrayList<Tour> tours = model.getObjectList();
                mTourAdapter.updateData(tours);
            }

            @Override
            public void onFailure(String error) {
                Snackbar.make(detail, error, Snackbar.LENGTH_SHORT);
            }
        });

    }

    public class TourAdapter extends BaseAdapter {

        ArrayList<Tour> tours;

        public TourAdapter(ArrayList<Tour> tours) {
            this.tours = tours == null ? new ArrayList<Tour>() : tours;
        }

        public void updateData(ArrayList<Tour> tours) {
            this.tours = tours == null ? new ArrayList<Tour>() : tours;
           notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return tours.size();
        }

        @Override
        public Object getItem(int position) {
            return tours.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            DropViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_drop_tour, null);
                holder = new DropViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (DropViewHolder) convertView.getTag();
            }

            Tour tour = tours.get(position);
            holder.tourName.setText(tour.getCity());
            return convertView;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tour, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Tour tour = tours.get(position);
            holder.tourName.setText(tour.getCity());
            return convertView;
        }

        public class ViewHolder {
            @Bind(R.id.tour_name)
            TextView tourName;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        public class DropViewHolder {
            @Bind(R.id.tour_name)
            TextView tourName;

            DropViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
